import axios from 'axios'

export function useApi(){
  const baseURL: string = (import.meta as any)?.env?.VITE_API_BASE || '/api'
  const client = axios.create({ baseURL })
  return {
    async uploadText(file: File){
      const fd = new FormData()
      fd.append('file', file)
      const res = await client.post('/texts/upload', fd, { headers: { 'Content-Type': 'multipart/form-data' } })
      return res.data
    },
    async getLabels(){
      const res = await client.get('/labels')
      return res.data
    },
    async createLabel(payload: any){
      const res = await client.post('/labels', payload)
      return res.data
    },
    async updateLabel(id: string, payload: any){
      const res = await client.put(`/labels/${id}`, payload)
      return res.data
    },
    async deleteLabel(id: string){
      const res = await client.delete(`/labels/${id}`)
      return res.data
    },
    async exportProject(payload?: any){
      const res = await client.post('/export/project', payload ?? {})
      return res.data
    },
    async exportAnnotations(){
      const res = await client.post('/export/annotations')
      return res.data
    },
    async exportLabelsExcel(){
      const res = await client.get('/export/labels/excel', { responseType: 'blob' })
      return res
    },
    async importProject(file: File){
      const fd = new FormData()
      fd.append('file', file)
      const res = await client.post('/import/project', fd, { headers: { 'Content-Type': 'multipart/form-data' } })
      return res.data
    }
  }
}
